//
//  LinkController.m
//  OCDemo
//
//  Created by BH on 15/10/12.
//  Copyright © 2015年 baohan. All rights reserved.
//

#import "LinkController.h"

@interface LinkController ()

@end

@implementation LinkController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(Class)labelClass
{
    return [MLLinkLabel class];
}

-(NSInteger)resultCount
{
    return 8;
}

-(void)changeToResult:(NSInteger)result
{
    self.label.textColor = [UIColor redColor];
    self.label.font = [UIFont systemFontOfSize:14.f];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.numberOfLines = 0;
    self.label.textInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    self.label.lineHeightMultiple = 1.0f;
    self.label.text = @"人生若只如初见，http://g.cn何事秋风悲http://baidu.com画扇。等闲变却故人心，dudl@qq.com却道故人心易变。13612341234骊山语罢清宵半，泪雨零铃终不怨。#何如 薄幸@锦衣郎，比翼连枝当日愿。";
    
    LABEL.beforeAddLinkBlock = nil;
    LABEL.dataDetectorTypes = MLDataDetectorTypeAll;
    LABEL.allowLineBreakInsideLinks = NO;  //link不允许换行显示
    LABEL.linkTextAttributes = nil;
    LABEL.activeLinkTextAttributes = nil;
    [LABEL setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
        
        UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:TIPS message:@"OK" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertCtrl addAction:okAction];
        [self presentViewController:alertCtrl animated:YES completion:nil];
    }];
    
    if (result == 0) {
        
    }else if (result == 1){
    
        self.label.lineHeightMultiple = 1.5f;
        
    }else if (result == 2){
        
        //更改链接样式
        self.label.textColor = [UIColor blueColor];
        LABEL.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};
        LABEL.activeLinkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName:[UIColor blackColor]};
        
    }else if (result == 3){
    
        //切换链接break换行
        LABEL.allowLineBreakInsideLinks = YES;
        
    }else if (result == 4){
    
        //添加链接,以及对链接单独设置长按事件
        MLLink *link = [MLLink linkWithType:MLLinkTypeURL value:@"http://www.sina.com" range:NSMakeRange(1, 2)];
        [link setDidLongPressLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {

            SHOW_SIMPLE_TIPS(TIPS);
        }];
        
    }else if (result == 5){
    
        //去除话题和@识别
        LABEL.dataDetectorTypes = MLDataDetectorTypeAll &~ MLDataDetectorTypeHashtag &~ MLDataDetectorTypeUserHandle;
        
    }else if (result == 6){
    
        //给一个含有链接的attrStr
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"人生若只如初见，何事秋风悲画扇。等闲变却故人心，却道故人心易变。骊山语罢清宵半，泪雨零铃终不怨。\n何如薄幸锦衣郎，比翼连枝当日愿。" ];
        [attrStr addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:NSMakeRange(0, 2)];
        [attrStr addAttribute:NSLinkAttributeName value:@"709407208@qq.com" range:NSMakeRange(3, 2)];
        [attrStr addAttribute:NSLinkAttributeName value:@"15090808843" range:NSMakeRange(10, 2)];
        LABEL.attributedText = attrStr;
        
        //在设置了text后针对修改link样式
        for (MLLink *link in LABEL.links) {
            if ([link.linkValue isEqualToString:@"15090808843"]) {
                link.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:0.607 blue:0.172 alpha:1.000]};
            }
        }
        //启用修改后的link样式
        [LABEL invalidateDisplayForLinks];
        
    }else if (result == 7){
    
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"人生若只如初见，何事秋风悲画扇。等闲变却故人心，却道故人心易变。骊山语罢清宵半，泪雨零铃终不怨。\n何如薄幸锦衣郎，比翼连枝当日愿。" ];
        [attrStr addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:NSMakeRange(0, 2)];
        [attrStr addAttribute:NSLinkAttributeName value:@"709407208@qq.com" range:NSMakeRange(3, 2)];
        [attrStr addAttribute:NSLinkAttributeName value:@"15090808843" range:NSMakeRange(10, 2)];
        
        //设置text之前针对自定义link样式
        [LABEL setBeforeAddLinkBlock:^(MLLink *link) {
            if (link.linkType == MLLinkTypeEmail) {
                link.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:0.961 green:0.292 blue:1.000 alpha:1.000]};
                link.activeLinkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName:[UIColor colorWithRed:0.211 green:0.753 blue:0.158 alpha:1.000]};
            }
        }];
        
        LABEL.attributedText = attrStr;
        
        //给一个含有链接的attrStr，并且自动检测其value所对应linkType
        LABEL.dataDetectorTypesOfAttributedLinkValue = MLDataDetectorTypeAll;
    }
    
    self.label.frameWidth = self.view.frameWidth - 10.f*2;
    [self.label sizeToFit];
}

@end
